package com.projects.android.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.projects.android.R;
import com.projects.android.bus.UsersBUS;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;

public class UpdateAccount extends Activity implements OnClickListener {
	private Intent intent;
	private Button btnCancel;
	private Button btnOK;

	private EditText txtName;
	private EditText txtPhone;
	private EditText txtAddress;

	// Use an existing ListAdapter that will map an array
	// of strings to TextViews
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.updateaccount);
		initFrm();
		Log.i("UpdateAccount Oncreate", "Create");
	}

	private void initFrm() {
		btnCancel = (Button) findViewById(R.id.btnUpdateAccountCancel);
		btnOK = (Button) findViewById(R.id.btnUpdateAccountOK);

		txtName = (EditText) findViewById(R.id.txtUpdateAccounrName);
		txtPhone = (EditText) findViewById(R.id.txtUpdateAccounrPhone);
		txtAddress = (EditText) findViewById(R.id.txtUpdateAccounrAddress);
		
		txtName.setText(SelectedItems.SelectedUsers.getName());
		txtPhone.setText(SelectedItems.SelectedUsers.getPhone());
		txtAddress.setText(SelectedItems.SelectedUsers.getAddress());
		
		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	private boolean updateAccount(){
		SelectedItems.SelectedUsers.setName(txtName.getText().toString());
		SelectedItems.SelectedUsers.setPhone(txtPhone.getText().toString());
		SelectedItems.SelectedUsers.setAddress(txtAddress.getText().toString());
		return UsersBUS.update(SelectedItems.SelectedUsers);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnUpdateAccountCancel:
			intent = Utils.browseIntent(this, "Intro");
			startActivity(intent);
			break;
		case R.id.btnUpdateAccountOK:
			if (!ValidateUpdate()) {
				return;
			}
			final ProgressDialog dialog = new ProgressDialog(this);
			dialog.setTitle("Loading. Please wait...");
			dialog.setIndeterminate(true);

			final Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						boolean result = updateAccount();
						private AlertDialog dlg;

						@Override
						public void run() {
							dialog.dismiss();
							Log.i("updateAccount result", String.valueOf(result));
							if(result){
								dlg = new AlertDialog.Builder(UpdateAccount.this).setTitle("Update information").setMessage("Update information successful")
								.setPositiveButton("OK", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int whichButton) {
										intent = Utils.browseIntent(UpdateAccount.this, "Intro");
										startActivity(intent);
									}
								}).create();
								dlg.show();						
							}else{
								dlg = Utils.showAlertDlg(UpdateAccount.this, "Update information", "Cannot update information");
								dlg.show();
							}
						}
					});
				}
			});
			dialog.show();
			thread.start();
			break;
		}
	}
	
	public boolean ValidateUpdate() {
		String errorTxt = "";
		if (txtName.getText().toString().equals("")) {
			errorTxt = "Name is empty";
		}
		if (txtPhone.getText().toString().equals("")) {
			errorTxt = "Phone is empty";
		}
		if (txtAddress.getText().toString().equals("")) {
			errorTxt = "Address is empty";
		}
		if (!errorTxt.equals("")) {
			AlertDialog dlg = Utils.showAlertDlg(this, "Validate UpdateAccount", errorTxt);
			dlg.show();
			return false;
		}
		return true;
	}
}
